package cn.com.allunion.sms.service.mybatis.impl;

import cn.com.allunion.sms.mapper.UserbaseAuthorityMapper;
import cn.com.allunion.sms.pojo.UserbaseAuthority;
import cn.com.allunion.sms.pojo.UserbaseAuthorityExample;
import cn.com.allunion.sms.service.mybatis.UserbaseAuthorityService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by YangJie on 2016/4/18.
 */
@Service
@Transactional
public class UserbaseAuthorityServiceImpl implements UserbaseAuthorityService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserbaseAuthorityServiceImpl.class) ;

    @Autowired
    UserbaseAuthorityMapper userbaseAuthorityMapper ;

    @Override
    public boolean save(UserbaseAuthority userbaseAuthority) {
        if (null == userbaseAuthority) {
            LOGGER.warn("the userbaseAuthority object is null.");
            return false ;
        }
        int result = userbaseAuthorityMapper.insert(userbaseAuthority) ;
        if (result > 0) {
            return true ;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<UserbaseAuthority> findByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            LOGGER.warn("the userId object is empty.");
            return null ;
        }

        UserbaseAuthorityExample example = new UserbaseAuthorityExample();
        UserbaseAuthorityExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId) ;
        return userbaseAuthorityMapper.selectByExample(example) ;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public long getCountByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            LOGGER.warn("the userId object is empty.");
            return 0 ;
        }

        UserbaseAuthorityExample example = new UserbaseAuthorityExample();
        UserbaseAuthorityExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId) ;
        return userbaseAuthorityMapper.countByExample(example) ;
    }
}
